package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.app.util.UploadUtil;
import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private  ITagService tagService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @GetMapping
    public Object getTravelWithState(StrategyQuery qo){
        PageInfo list = strategyService.selectForList(qo);
        return list;
    }

    @GetMapping("/{strategyId}")
    public Object getTitleByRegionId(@PathVariable Long strategyId){
        Strategy strategy = strategyService.getTitleByStrategyId(strategyId);
        return strategy;
    }

    @GetMapping("/{strategyId}/travels")
    public Object getTravelByStrategyId(@PathVariable Long strategyId){
        return travelService.getTravelByStrategyId(strategyId);
    }

    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogsByStrategyId(@PathVariable Long strategyId){
        return catalogService.getCatalogsByStrategyId(strategyId);
    }

    /**
     *  添加点评评论的保存操作
     * @param strategyComment
     * @return
     */
    @PostMapping("{strategyId}/comments")
    public Object insertByComment(StrategyComment strategyComment,MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                //String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PAGE + "upload");
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                strategyComment.setImgUrls(imgUrl);
            }
            strategyComment.setCommendTime(new Date());
            //获取到点前用户的id值
            strategyComment.setCreateTime(new Date());
            User currentUser = UserContext.getCurrentUser();
            //添加user对象
            strategyComment.setUser(currentUser);
            //进行便签的遍历操作
           // Long[] tags = strategyComment.getTag().getTags();
            //获取到评论id值
            //Long commentId = strategyComment.getId();
            //遍历操作
            strategyCommentService.insert(strategyComment);
            result.setData(strategyComment.getId());

               /* tagService.insertByTags(tag,commentId);
            }*/
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
       return result;
    }
}
